import Vue from 'vue'
import Router from 'vue-router'
// import { tranListTotreeList } from '@/utils'
Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// import BlankLayout from '@/layout/BlankLayout'
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: 'dashboard', icon: 'el-icon-s-home', affix: true, roles: ['admin', 'member'] }
      }
    ]
  },
  {
    path: '/deviceList',
    component: Layout,
    children: [
      {
        path: 'deviceList',
        component: () => import('@/views/ManagementSystem/deviceList'),
        name: 'deviceList',
        meta: { title: '设备管理', icon: 'el-icon-mobile-phone', affix: false, roles: ['admin', 'member'] }
      }
    ]
  },
  {
    path: '/storeList',
    component: Layout,
    children: [
      {
        path: 'storeList',
        component: () => import('@/views/ManagementSystem/storeList'),
        name: 'storeList',
        meta: { title: '店铺管理', icon: 'el-icon-s-shop', affix: false, roles: ['admin', 'member'] }
      }
    ]
  },
  {
    path: '/taskListPublic',
    component: Layout,
    children: [
      {
        path: 'taskListPublic',
        component: () => import('@/views/ManagementSystem/taskListPublic'),
        name: 'taskListPublic',
        meta: { title: '任务管理', icon: 'el-icon-notebook-1', affix: false, roles: ['admin', 'member'] }
      }
    ]
  },
  {
    path: '/taskRecordList',
    component: Layout,
    children: [
      {
        path: 'taskRecordList',
        component: () => import('@/views/ManagementSystem/taskRecordList'),
        name: 'taskRecordList',
        meta: { title: '任务记录', icon: 'el-icon-notebook-2', affix: false, roles: ['admin', 'member'] }
      }
    ]
  },
  {
    path: '/evaluateList',
    component: Layout,
    children: [
      {
        path: 'evaluateList',
        component: () => import('@/views/ManagementSystem/evaluateList'),
        name: 'evaluateList',
        meta: { title: '评价列表', icon: 'el-icon-chat-line-square', affix: false, roles: ['admin', 'member'] }
      }
    ]
  },
  {
    path: '/transLink',
    component: Layout,
    children: [
      {
        path: 'transLink',
        component: () => import('@/views/ManagementSystem/transLink'),
        name: 'transLink',
        meta: { title: '二维码转链接', icon: 'el-icon-discover', affix: false, roles: ['admin', 'member'] }
      }
    ]
  },
  {
    path: '/accountManae',
    component: Layout,
    children: [
      {
        path: 'accountManae',
        component: () => import('@/views/ManagementSystem/accountManae'),
        name: 'accountManae',
        meta: { title: '账号管理', icon: 'el-icon-discover', affix: false, roles: ['admin', 'member'] }
      }
    ]
  }
]

export const asyncRoutes = [
  // { path: '*', redirect: '/404', hidden: true }
  {
    path: '/ManagementSystem',
    component: Layout,
    meta: { title: '系统管理', icon: 'el-icon-s-custom', roles: ['admin'] },
    redirect: 'memberList',
    alwaysShow: true, // 是否显示
    children: [
      {
        path: 'memberList',
        component: () => import('@/views/ManagementSystem/MemberList'),
        name: 'memberList',
        meta: { title: '会员管理', noCache: false, roles: ['admin'] }
      }
      // {
      //   path: 'deviceList',
      //   component: () => import('@/views/ManagementSystem/deviceList'),
      //   name: 'deviceList',
      //   meta: { title: '设备管理', noCache: false, roles: ['admin', 'member'] }
      // },
      // {
      //   path: 'storeList',
      //   component: () => import('@/views/ManagementSystem/storeList'),
      //   name: 'storeList',
      //   meta: { title: '店铺管理', noCache: false, roles: ['admin', 'member'] }
      // },
      // {
      //   path: 'taskListPublic',
      //   component: () => import('@/views/ManagementSystem/taskListPublic'),
      //   name: 'taskListPublic',
      //   meta: { title: '任务管理', noCache: false, roles: ['admin', 'member'] }
      // },
      // {
      //   path: 'taskRecordList',
      //   component: () => import('@/views/ManagementSystem/taskRecordList'),
      //   name: 'taskRecordList',
      //   meta: { title: '任务记录', noCache: false, roles: ['admin', 'member'] }
      // }
    ]
  }
  // {
  //   path: '/bigScreen',
  //   component: BlankLayout,
  //   redirect: 'overview',
  //   name: 'bigScreen',
  //   meta: {
  //     title: '大屏监测',
  //     icon: 'dashboard'
  //   },
  //   children: [
  //     {
  //       path: 'overview',
  //       component: () => import('@/views/bigScreen/overview'),
  //       name: 'overview',
  //       meta: { title: '综合概况', icon: 'dashboard', noCache: true, roles: ['admin'] }
  //     }
  //   ]
  // }
]
// const menuList = JSON.parse(localStorage.getItem('MENU_LIST'))?.value.list
// console.log('menuList', menuList)
// if (menuList) {
//   console.log('menuList 转之后', tranListTotreeList(menuList))
//   tranListTotreeList(menuList).forEach(v => {
//     const tempObj = {
//       path: v.menuPath,
//       component: Layout,
//       alwaysShow: true,
//       meta: {
//         title: v.title,
//         icon: v.icon,
//         affix: v.affix === '1',
//         noCache: v.noCache === '1'
//       },
//       children: []
//     }
//     if (v.redirect) {
//       tempObj.redirect = v.redirect
//     }
//     if (v.children.length) {
//       v.children.forEach(c => {
//         tempObj.children.push({
//           path: c.menuPath,
//           component: () => import('@/views/ManagementSystem/MemberList'),
//           meta: {
//             title: c.title,
//             icon: c.icon,
//             affix: c.affix === '1',
//             noCache: c.noCache === '1'
//           }
//         })
//       })
//     }
//     // console.log('tempObj', tempObj)
//     asyncRoutes.push(tempObj)
//   })
// }
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: [...constantRoutes, ...asyncRoutes]
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
